package com.example.customviewprojdct.view.other

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.animation.DecelerateInterpolator
import com.example.customviewprojdct.extensions.dp
import kotlin.math.max
import kotlin.math.min


/**
 * 相对进度条
 */
class RelativeProgressView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    d: Int = 0
) : View(context, attrs, d) {
    private var maxProgress = 100F
    private var offsetX = 2.dp  //错切长度
    private val dashPathLeft = Path()
    private val dashPathRight = Path()
    private val progressPaintLeft = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.parseColor("#12D9AA")
        style = Paint.Style.FILL
    }
    private val progressPaintRight = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.parseColor("#FF00BA")
        style = Paint.Style.FILL
    }

    private var arcWidth: Float = 0F

    var progress = 0F
        set(value) {
            field = value
            reMeasureProgressPath()
            invalidate()
        }
    var animatorProgress = 1F   //右侧进度动画

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        progressPaintLeft.shader = LinearGradient(
            0f, 0f, width.toFloat(), 0f, Color.parseColor("#FF453A"),
            Color.parseColor("#FF7B6C"), Shader.TileMode.CLAMP
        )
        progressPaintRight.shader = LinearGradient(
            0f, 0f, width.toFloat(), 0f, Color.parseColor("#63E6E2"),
            Color.parseColor("#30B0C7"), Shader.TileMode.CLAMP
        )
        arcWidth = (height / 2).toFloat()
    }

    private fun reMeasureProgressPath() {
        //左侧进度
        dashPathLeft.reset()
        if (progress > 0) {
            val leftProgressWidth = (progress / maxProgress * width)
            dashPathLeft.moveTo(arcWidth, 0f)
            dashPathLeft.arcTo(0f, 0f, height.toFloat(), height.toFloat(), -90f, -180f, false)
            dashPathLeft.lineTo(max(leftProgressWidth - offsetX, arcWidth), height.toFloat())
            dashPathLeft.lineTo((leftProgressWidth - offsetX).coerceAtLeast(arcWidth), height.toFloat())
            if (progress == maxProgress) {  //满进度
                dashPathLeft.arcTo(width.toFloat() - height.toFloat(), 0f, width.toFloat(), height.toFloat(), -270f, -180f, false)
            } else {
                dashPathLeft.lineTo(leftProgressWidth.coerceAtLeast(arcWidth), 0f)
            }
            dashPathLeft.close()
        }

        //右侧进度
        dashPathRight.reset()
        if (progress < maxProgress) {
            val rightProgressWidth = (width - (progress / maxProgress * width)) * animatorProgress
            val rightProgressWidth2 = (width - (progress / maxProgress * width))
            Log.d("baize_animator", "rightProgressWidth:$rightProgressWidth2 -> animatorProgress$rightProgressWidth ::$animatorProgress ")
            dashPathRight.moveTo(width.toFloat() - arcWidth, 0f)
            dashPathRight.arcTo(width.toFloat() - height.toFloat(), 0f, width.toFloat(), height.toFloat(), -90f, 180f, false)
            dashPathRight.lineTo(min(width.toFloat() - rightProgressWidth, width.toFloat() - arcWidth), height.toFloat())
            if (progress == 0f) { //满进度
                dashPathRight.arcTo(width.toFloat() - rightProgressWidth, 0f, width.toFloat() - rightProgressWidth + height.toFloat(), height.toFloat(), -270f, 180f, false)
            } else {
                dashPathRight.lineTo(min(width - rightProgressWidth + offsetX, width - arcWidth), 0f)
            }
            dashPathRight.close()
        }
    }

    override fun onDraw(canvas: Canvas) {
        canvas.drawPath(dashPathLeft, progressPaintLeft)
        canvas.drawPath(dashPathRight, progressPaintRight)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val height = resolveSize(12.dp.toInt(), heightMeasureSpec)
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), height)
    }

    fun startAnimatorProgress(progress: Float, animator: Boolean = false) {
        if (animator) {
            val animator = ObjectAnimator.ofFloat(this, "progress", progress)
            val rightProgressAnimator = ObjectAnimator.ofFloat(this, "animatorProgress", 0F, 1F)

            val animatorSet = AnimatorSet()
            animatorSet.duration = 500
            animatorSet.interpolator = DecelerateInterpolator()
            animatorSet.playTogether(animator, rightProgressAnimator)
            animatorSet.start()
        } else {
            this.animatorProgress = 1F
            this.progress = progress
        }
    }
}